def is_prime(n):
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

def max_prime(number):
    if len(str(number)) != 4 or number < 1000:
        return 0
    d = str(number)
    mnum = 0
    for i in range(4):
        for j in range(i + 1, 5):
            num = int(d[i:j])
            if is_prime(num) and num > mnum:
                mnum = num
    return mnum

number = int(input())
res = max_prime(number)
print(res)
